import request from '@/utils/request'

// 获取微信JSSDK配置
export function getWxConfig() {
  return request({
    url: '/system/wx/config',
    method: 'get',
    params: {
      url: window.location.href.split('#')[0]
    }
  })
}

// 配置微信JSSDK
export function configWxJsSdk() {
  return new Promise((resolve, reject) => {
    getWxConfig().then(res => {
      if (res.code === 200) {
        const config = res.data
        wx.config({
          debug: false,
          appId: config.appId,
          timestamp: config.timestamp,
          nonceStr: config.nonceStr,
          signature: config.signature,
          jsApiList: ['scanQRCode']
        })

        wx.ready(() => {
          resolve()
        })

        wx.error((err) => {
          console.error('微信JSSDK配置失败：', err)
          reject(err)
        })
      } else {
        reject(new Error('获取微信配置失败'))
      }
    }).catch(err => {
      reject(err)
    })
  })
} 